package com.amaker.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.amaker.bean.Stu;
import com.amaker.dao.StuDao;
import com.amaker.util.DBUtil;

public class StuDaoImpl implements StuDao {

	@Override
	public void save(Stu s) {
		String sql="insert into StuTbl (name,age) values (?,?)";
		DBUtil util=new DBUtil();
		Connection conn=util.getConnection();
		try {
	    PreparedStatement ps=conn.prepareStatement(sql);
	    ps.setString(1, s.getName());
	    ps.setLong(2, s.getAge());
	    ps.executeUpdate();
    } catch (SQLException e) {
	    e.printStackTrace();
    } finally {
    	try {
	      conn.close();
      } catch (SQLException e) {
	      e.printStackTrace();
      }
    }
	}

	@Override
	public List<Stu> list() {
		String sql="select id,name,age from StuTbl";
		DBUtil util=new DBUtil();
		Connection conn=util.getConnection();
		List<Stu> list=null;
		try {
			Statement sm = conn.createStatement();
			ResultSet rs=sm.executeQuery(sql);
	    while(rs.next()){
	    	Stu stu=new Stu();
	    	stu.setId(rs.getInt(1));
	    	stu.setName(rs.getString(2));
	    	stu.setAge(rs.getInt(3));
	    	list.add(stu);
	    }
	    return list;
    } catch (SQLException e) {
	    e.printStackTrace();
    }
		
		return null;
	}

}
